package com.bibrApp.difu.actvity.beginner_guide_mvp;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bibrApp.difu.Config;
import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.actvity.certificate_mvp.CertificateActivity;
import com.bibrApp.difu.actvity.df_u_mvp.UActivity;
import com.bibrApp.difu.actvity.fund_security_code_mvp.FundSecurityCodeActivity;
import com.bibrApp.difu.actvity.login_mvp.LoginActivity;
import com.bibrApp.difu.actvity.pay_setting_mvp.PaySettingActivity;
import com.bibrApp.difu.actvity.register_mvp.RegisterActivity;
import com.bibrApp.difu.actvity.security_setting_mvp.SecuritySettingActivity;
import com.bibrApp.difu.actvity.wallet_mvp.WalletActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.utils.BiBrShare;
import com.bibrApp.difu.utils.IntentUtil;
import com.bibrApp.difu.utils.ShareUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

public class BeginnerGuideActivity extends BaseActivity<BeginnerGuidePresenter> implements IBeginnerGuideView {

    private ImmersionBar mImmersionBar;
    private Context mContext;
    @BindView(R.id.tv_guide_register)
    TextView tv_guide_register;
    @BindView(R.id.tv_security_code)
    TextView tv_security_code;
    @BindView(R.id.tv_guide_certificate)
    TextView tv_guide_certificate;
    @BindView(R.id.tv_guide_bind_address)
    TextView tv_guide_bind_address;
    @BindView(R.id.tv_guide_money)
    TextView tv_guide_money;
    @BindView(R.id.tv_guide_u)
    TextView tv_guide_u;

    @BindView(R.id.guide_register)
    RelativeLayout guide_register;
    @BindView(R.id.guide_security_code)
    RelativeLayout security_code;
    @BindView(R.id.guide_certificate)
    RelativeLayout guide_certificate;
    @BindView(R.id.guide_bind_address)
    RelativeLayout guide_bind_address;
    @BindView(R.id.guide_money)
    RelativeLayout guide_money;
    @BindView(R.id.guide_u)
    RelativeLayout guide_u;
    private ShareUtils<LoginResponseBody> biBrShare;
    private static final int CODE_REQUEST = 0x112;

    @Override
    protected void initPresenter() {
        presenter = new BeginnerGuidePresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_beginner_guide;
    }

    @Override
    public void initView() {
        mContext = this;
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init();
        biBrShare = BiBrShare.getShare(mContext, Config.FILE_ACCOUNT);

        updateView();
    }


    @Override
    public void error(String msg) {
        ToastUtils.ShowToast(mContext, msg);
    }

    @Override
    public void success(String msg) {
        ToastUtils.ShowToast(mContext, msg);
        biBrShare.clearAll();

        DfApp app = (DfApp) getApplication();
        app.setuId(null);
        app.setUserId(null);
        app.setToken(null);
        app.setmData(null);
        app.setNickImage(null);
        app.setNickName(null);

        IntentUtil.toActivity(LoginActivity.class, this);
        finish();
    }

    @Override
    public void successOfGetPaySetting(boolean hasPaySetting) {
        if(hasPaySetting) {
            tv_guide_bind_address.setText("已完成");
            tv_guide_bind_address.setCompoundDrawables(null, null, null, null);
            guide_bind_address.setClickable(false);
        }
    }

    @Override
    public void successOfGetAllAssets(boolean hasMoney) {
        if(hasMoney){
            tv_guide_money.setText("已完成");
            tv_guide_money.setCompoundDrawables(null, null, null, null);
            guide_money.setClickable(false);
        }
    }

    @Override
    public void successOfGetUserInfo(LoginResponseBody.CenterUserMain userInfo) {
        LoginResponseBody data = app.getmData();
        if (userInfo != null) {
            biBrShare.deleteData(Config.FILE_ACCOUNT_KEY);
            data.setCenterUserMain(userInfo);
            biBrShare.saveData(data, Config.FILE_ACCOUNT_KEY);
        }
        updateView();
    }

    private void updateView() {
        DfApp dfApp = (DfApp) getApplication();
        LoginResponseBody loginResponseBody = dfApp.getmData();
        if(loginResponseBody != null){
            tv_guide_register.setText("已完成");
            tv_guide_register.setCompoundDrawables(null,null,null,null);
            guide_register.setClickable(false);
            if(!TextUtils.isEmpty(loginResponseBody.getCenterUserMain().getTradePassword())){
                tv_security_code.setText("已完成");
                tv_security_code.setCompoundDrawables(null,null,null,null);
                security_code.setClickable(false);
            }/*else {
                tv_security_code.setText("未完成");
                tv_security_code.setCompoundDrawables(null,null,getResources().getDrawable(R.mipmap.icon_mor),null);
            }*/
            if(loginResponseBody.getCenterUserMain().getSeniorCertification() ==3){
                tv_guide_certificate.setText("已完成");
                tv_guide_certificate.setCompoundDrawables(null,null,null,null);
                guide_certificate.setClickable(false);
            }/*else{
                tv_guide_certificate.setText("未完成");
                tv_guide_certificate.setCompoundDrawables(null,null,getResources().getDrawable(R.mipmap.icon_mor),null);
            }*/
            if(TextUtils.isEmpty(dfApp.getmData().getCenterUserMain().getSerialNumber())){
                tv_guide_u.setText("已完成");
                tv_guide_u.setCompoundDrawables(null,null,null,null);
                guide_u.setClickable(false);
            }/*else{
                tv_guide_u.setText("未完成");
                tv_guide_u.setCompoundDrawables(null,null,getResources().getDrawable(R.mipmap.icon_mor),null);
            }*/
            presenter.getAllPaySetting();
            presenter.queryAllAssets();
        }
    }

    @OnClick(R.id.return_iv)
    public void onBackClick(){
        finish();
    }

    @OnClick(R.id.guide_register)
    public void registerClick(){
        IntentUtil.toActivityForResult(RegisterActivity.class,this,CODE_REQUEST);
    }

    @OnClick(R.id.guide_security_code)
    public void setSecurityCodeClick(){
        IntentUtil.toActivityForResult(FundSecurityCodeActivity.class,this,CODE_REQUEST);
    }

    @OnClick(R.id.guide_certificate)
    public void certificateClick(){
        IntentUtil.toActivityForResult(CertificateActivity.class,this,CODE_REQUEST);
    }

    @OnClick(R.id.guide_bind_address)
    public void bindAddressClick(){
        IntentUtil.toActivityForResult(PaySettingActivity.class,this,CODE_REQUEST);
    }

    @OnClick(R.id.guide_money)
    public void goRechargeClick(){
        IntentUtil.toActivityForResult(WalletActivity.class,this,CODE_REQUEST);
    }

    @OnClick(R.id.guide_u)
    public void getUClick(){
        IntentUtil.toActivityForResult(UActivity.class,this,CODE_REQUEST);
    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == CODE_REQUEST){
            presenter.queryAllAssets();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }

    }

}
